package com.ruoyi.project.data.dataManager.domain;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

public class SocketClient {

	private int intConnectCount = 0; // 连接次数���
	private Socket clientSocket = null;
	// define a Socket that is used for call server socket.
	private ObjectInputStream transportReceive = null;
	// used for recieve data from server.
	private ObjectOutputStream transportSend = null;
	// used for send data to server.
	private OutputStream _os = null;
	private InputStream _is = null;
	private boolean KeepRunning = false;
	// a flag which describes if the current thread is running or not.
	private String serverName = null; // server name/ip.
	private int serverPort = 0; // server port number.
	private int timeOut = 0; // timeout of connection
	private boolean connect = false; // determnie if connect the server or not
	private Map<String, Object> requestInfo = null;

	

	/**
	 * default constructor
	 */
	public SocketClient(Map<String, Object> requestInfo, String ip, Integer port) {
		this.requestInfo = requestInfo;
		this.serverName = ip;
		this.serverPort = port;
		this.timeOut = 60;
		this.KeepRunning = true;
	}

	
	/**
	 * create a new thread that will used to call server.
	 */
	public void transport() throws Exception {
		while (KeepRunning) {

			if (!isConnect()) { // 不让连接超过2次��
				if (intConnectCount > 2)
					break;
				else
					ConnectRemote();
			} else {
				try {
					transportSend.writeObject(requestInfo);
					//System.out.println("上传请求数据包完毕");
				} catch (IOException excpt) {
					//System.out.println("上传请求数据包时出错：\n" + excpt.getMessage());
					throw excpt;
				}

				disConnectRemote();
				KeepRunning = false;
			}
		}
	}

	/**
	 * connect to server.
	 */
	private void ConnectRemote() throws Exception {
		try {
			if (requestInfo == null) {
				requestInfo = new HashMap<String, Object>();
			}
			intConnectCount++;
			//System.out.println("Socket通信开始...");

			// create a new socket to connect to server.
			//System.out.println("name IS : " + serverName);
			//System.out.println("PORT IS : " + serverPort);

			clientSocket = new Socket(serverName, serverPort);

			clientSocket.setSoTimeout(timeOut);
			//System.out.println("server:  " + clientSocket);

			// construct I/O object, which used for read/write data.
			transportSend = new ObjectOutputStream(clientSocket
					.getOutputStream());

			transportReceive = new ObjectInputStream(clientSocket
					.getInputStream());

			// set the connect flag to true.
			if (clientSocket != null && transportSend != null
					&& transportReceive != null) {
				connect = true;
				//System.out.println("Socket连接创建成功");
			}
		} catch (ConnectException excpt) {
			//System.out.println("连接" + serverName + "时出错:\n"
			//		+ excpt.getMessage());
			this.disConnectRemote();
			throw excpt;
		} catch (UnknownHostException excpt) {
			//System.out.println("没有发现" + serverName + " 或" + serverName
			//		+ "网络故障:\n" + excpt.getMessage());
			this.disConnectRemote();
			throw excpt;
		} catch (IOException excpt) {
			//System.out.println(serverName + "  I/O错:\n " + excpt.getMessage());
			//System.out.println("没有发现" + serverName + "， 或" + serverName
			//		+ "网络故障:\n" + excpt.getMessage());
			this.disConnectRemote();
			throw excpt;
		}
	}

	/**
	 * determnie if connect the server or not.
	 * 
	 * @return
	 */
	public boolean isConnect() {

		return connect;
	}

	/**
	 * disconnect to server, and close streams and socket.
	 */
	public void disConnectRemote() throws Exception {
		try {

			// close streams and socket
			if (transportSend != null) {
				transportSend.close();
			}

			if (transportReceive != null) {
				transportReceive.close();
			}

			if (_is != null) {
				_is.close();
			}
			if (_os != null) {
				_os.close();
			}
			if (clientSocket != null) {
				clientSocket.close();
			}
		} catch (IOException excpt) {
			throw new Exception("关闭streams和socket时出错");
		}

		//System.out.println("关闭streams和socket");
	}

	public static void main(String args[]) {
		try {
			// Socket socket = new Socket("10.12.1.73",8878);
			Map<String, Object> transportInfo = new HashMap<String, Object>();

			transportInfo.put("dataType", "ocr");
			// transportInfo.put("dataType","imp");
			transportInfo.put("message", "restart");
			// transportInfo.put("message","timerend");
			transportInfo.put("trdate", "2010-05-13");
			SocketClient client = new SocketClient(transportInfo, "10.12.1.73",8878);
			client.transport();
			// ObjectOutputStream oos =
			// new ObjectOutputStream(socket.getOutputStream());
			// oos.writeObject(transportInfo);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
